package ndquangr.intern.ringtonemaker.listitems;

import java.util.ArrayList;

import ndquangr.intern.ringtonemaker.AllSongListActivity;
import ndquangr.intern.ringtonemaker.BrowseActivity;
import ndquangr.intern.ringtonemaker.Constants;
import ndquangr.intern.ringtonemaker.R;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class BrowseCateAdapter extends ArrayAdapter<String> {
	private Context m_context;
	private ArrayList<String> items;
	private int cateTag;
	private String currentViewList;
		
	public BrowseCateAdapter(Context context, int textViewResourceId, ArrayList<String> items) {
        super(context, textViewResourceId, items);
        currentViewList = items.remove(0);
        this.items = items;    
        m_context = context;
	}
	
	private OnClickListener mBrowseClickListener = new OnClickListener() {				
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			cateTag = ((Integer) v.getTag()).intValue();
			if (cateTag == 0) {
				goAllSongList();
			} else if (cateTag > 0 && cateTag < 6) {
				goSortedList(cateTag);
			} else {
				return;
			}
		}			
	};
	
	private OnClickListener mBrowseSpecificClickListener = new OnClickListener() {				
		@Override
		public void onClick(View v) {			
			// TODO Auto-generated method stub
			if (currentViewList != null && !currentViewList.equals("")) {
				Intent myIntent = new Intent(m_context, AllSongListActivity.class);
				myIntent.putExtra("category", currentViewList);
				myIntent.putExtra("category_name", v.getTag().toString());
				m_context.startActivity(myIntent);
			}
		}			
	};
		
	   
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.browse_cate_item, null);
            }
            String o = items.get(position);
            if (o != null) {
                    TextView tv_item = (TextView) v.findViewById(R.id.tv_browse_cate_item);                    
                    ImageView btn_view = (ImageView) v.findViewById(R.id.btn_browse_cate_view);
                    btn_view.setTag(position);                    
                    
                    tv_item.setText(o);            
                    if (currentViewList.equals("")) {
                    	btn_view.setOnClickListener(mBrowseClickListener);
                    } else {
                    	btn_view.setOnClickListener(mBrowseSpecificClickListener);
                    	btn_view.setTag(o);
                    }
            }
            return v;
    }


    // Go to get the specific categories
	protected void goSortedList(int tag) {
		// TODO Auto-generated method stub
		Intent myIntent = new Intent(m_context, BrowseActivity.class);
		myIntent.putExtra("tag", Constants.MUSIC_CATEGORIES[tag]);
		m_context.startActivity(myIntent);
	}

	// Go to view all song;
	protected void goAllSongList() {
		// TODO Auto-generated method stub
		Intent myIntent = new Intent(m_context, AllSongListActivity.class);
		m_context.startActivity(myIntent);
	}
}
